import { createRouter, createWebHashHistory } from 'vue-router'
import { getToken, isAdmin } from '@/utils/auth'
import { ElMessage } from 'element-plus'

const routes = [
  {
    path: '/login',
    name: 'login',
    meta: {
      title: "秒杀系统-登录页面"
    },
    component: () => import('@/views/LoginView.vue')
  },
  {
    path: '/seckill/:id',
    name: 'SeckillDetail',
    meta: {
      title: "秒杀系统-秒杀详情"
    },
    component: () => import('@/views/front/SeckillDetail.vue')
  },  
  {
    path: '/seckill/order/pay',
    name: 'SeckillOrderPay',
    meta: {
      title: "秒杀系统-订单支付"
    },
    component: () => import('@/views/front/SeckillOrderPay.vue')
  },
  {
    path: '/seckill/orders',
    name: 'SeckillOrderList',
    component: () => import('../views/front/SeckillOrderList.vue')
  },
  {
    path: '/payment/success',
    name: 'PaymentSuccess',
    component: () => import('../views/front/PaymentSuccess.vue')
  },
  {
    path: '/user',
    name: 'UserCenter',
    component: () => import('../views/front/UserCenter.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },
  // 管理员路由
  {
    path: '/manager',
    name: 'ManagerLayout',
    component: () => import('@/views/manager/ManagerLayout.vue'),
    meta: { 
      requiresAuth: true,
      requiresAdmin: true 
    },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/manager/Dashboard.vue'),
        meta: { title: '管理面板' }
      },
      {
        path: 'products',
        name: 'ProductManager',
        component: () => import('@/views/manager/ProductManager.vue'),
        meta: { title: '商品管理' }
      },
      {
        path: 'orders',
        name: 'OrderManager',
        component: () => import('@/views/manager/OrderManager.vue'),
        meta: { title: '订单管理' }
      },
      {
        path: 'users',
        name: 'UserManager',
        component: () => import('@/views/manager/UserManager.vue'),
        meta: { title: '用户管理' }
      },
      {
        path: 'coupons',
        name: 'CouponManager',
        component: () => import('@/views/manager/CouponManager.vue'),
        meta: { title: '优惠券管理' }
      },
      {
        path: 'user-coupons',
        name: 'UserCouponManager',
        component: () => import('@/views/manager/UserCouponManager.vue'),
        meta: { title: '用户优惠券管理' }
      },
      {
        path: 'seckill-product',
        name: 'SeckillProductManager',
        component: () => import('@/views/manager/SeckillProductManager.vue'),
        meta: { title: '秒杀商品管理' }
      }
    ]
  },
  
  // 用户路由
  {
    path: '/',
    name: 'UserLayout',
    component: () => import('@/views/front/UserLayout.vue'),
    children: [
      {
        path: '',
        name: 'home',
        meta: {
          title: "秒杀系统-首页"
        },
        component: () => import('../views/front/HomeView.vue')
      }
    ]
  },
  {
    path: '/seckill',
    name: 'SeckillIndex',
    component: () => import('@/views/front/SeckillIndex.vue'),
    meta: { title: '限时秒杀' }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // 检查页面是否需要登录权限
  if (to.matched.some(record => record.meta.requiresAuth)) {
    const token = getToken()
    if (!token) {
      ElMessage.warning('请先登录')
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
      return
    }

    // 检查是否需要管理员权限
    if (to.matched.some(record => record.meta.requiresAdmin)) {
      if (!isAdmin()) {
        ElMessage.error('无权访问')
        next('/')
        return
      }
    }
  }
  
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title
  }
  
  next()
})

export default router
